#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //控制数字按键
    connect(ui->btn0,&QPushButton::clicked,[this](){this->setNum("0");});
    connect(ui->btn1,&QPushButton::clicked,[this](){this->setNum("1");});
    connect(ui->btn2,&QPushButton::clicked,[this](){this->setNum("2");});
    connect(ui->btn3,&QPushButton::clicked,[this](){this->setNum("3");});
    connect(ui->btn4,&QPushButton::clicked,[this](){this->setNum("4");});
    connect(ui->btn5,&QPushButton::clicked,[this](){this->setNum("5");});
    connect(ui->btn6,&QPushButton::clicked,[this](){this->setNum("6");});
    connect(ui->btn7,&QPushButton::clicked,[this](){this->setNum("7");});
    connect(ui->btn8,&QPushButton::clicked,[this](){this->setNum("8");});
    connect(ui->btn9,&QPushButton::clicked,[this](){this->setNum("9");});
    //控制点按键
    connect(ui->dian,&QPushButton::clicked,[this](){this->setNum(".");});

    //控制运算符按键
    connect(ui->jia,&QPushButton::clicked,[this](){this->setOperator("+");});
    connect(ui->jian,&QPushButton::clicked,[this](){this->setOperator("-");});
    connect(ui->cheng,&QPushButton::clicked,[this](){this->setOperator("*");});
    connect(ui->chu,&QPushButton::clicked,[this](){this->setOperator("/");});



}

//操作数num1 num2
void Widget::setNum(QString n)
{
    //对. 的处理
    if(ui->lineEdit->text().isEmpty() && n==".")
    {
        return;
    }
    if(ui->lineEdit->text().contains(".") && n==".")
    {
        return;
    }

    //对0的处理
    if(ui->lineEdit->text().isEmpty() && n=="0")
    {
        n="0.";
    }

    //运算符为空设置num1
    if(this->opt.isEmpty())
    {
        this->num1+=n;
        ui->lineEdit->setText(num1);
    }
    else {
        this->num2+=n;
        ui->lineEdit->setText(num2);
    }

}

//操作符
void Widget::setOperator(QString opt)
{
    this->optNum+=1;
    if(this->optNum <= 1)
    {
        //保存运算符
        this->opt=opt;
        //清空屏幕
        ui->lineEdit->setText("");
    }
    else
    {
        qDebug()<<this->num1<<this->opt<<this->num2;
        double result=0.0;
        if(this->opt=="+")
        {
            result=this->num1.toDouble()+this->num2.toDouble();
        }
        else if(this->opt=="-")
        {
            result=this->num1.toDouble()-this->num2.toDouble();
        }
        else if(this->opt=="*")
        {
            result=this->num1.toDouble()*this->num2.toDouble();
        }
        else if(this->opt=="/")
        {
            result=this->num1.toDouble()/this->num2.toDouble();
        }
        ui->lineEdit->setText(QString::number(result));
        //结果给num1 清空num2 清空运算符 可以连续运算
        this->num1=QString::number(result);
        //保存运算符
        this->opt=opt;
        this->num2="";
    }


}


Widget::~Widget()
{
    delete ui;
}

//等号
void Widget::on_deng_clicked()
{
    qDebug()<<this->num1<<this->opt<<this->num2;
    double result=0.0;
    if(this->opt=="+")
    {
        result=this->num1.toDouble()+this->num2.toDouble();
    }
    else if(this->opt=="-")
    {
        result=this->num1.toDouble()-this->num2.toDouble();
    }
    else if(this->opt=="*")
    {
        result=this->num1.toDouble()*this->num2.toDouble();
    }
    else if(this->opt=="/")
    {
        result=this->num1.toDouble()/this->num2.toDouble();
    }
    ui->lineEdit->setText(QString::number(result));
    //结果给num1 清空num2 清空运算符 可以连续运算
    this->num1=QString::number(result);
    this->num2="";
    this->opt="";
    this->optNum=0;
}

//C归零

void Widget::on_pushButton_clicked()
{
    this->num1="";
    this->num2="";
    this->opt="";
    this->optNum=0;
    ui->lineEdit->setText("");
}

//back按键
void Widget::on_back_clicked()
{
    if(this->opt.isEmpty())
    {
        this->num1=num1.mid(0,this->num1.size()-1);
        ui->lineEdit->setText(this->num1);
    }
    else
    {
        this->num2=num2.mid(0,this->num2.size()-1);
        ui->lineEdit->setText(this->num2);
    }
}
